// pages/bill/bill.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    totalPrice: 0,
    totalDisPrice: 0,
    date: '日期', //日期选择器的时间
    endNow: new Date().getFullYear() + '-' + parseInt(new Date().getMonth() + 1) + '-' + new Date().getDate(), //时间选择器末时间

    imgList1: '../../images/downImg.png',
    imgList2: '../../images/downImg.png',
    imgList3: '../../images/downImgW.png',
    typeList1: {
      showList1: true,
      titleList1: '全部类型',
      child: [{
          title: '全部类型',
          selected: true
        },
        {
          title: '手机',
          selected: false
        }, {
          title: '电脑周边',
          selected: false
        }, {
          title: '五金/工具',
          selected: false
        }
      ]
    },
    typeList2: {
      showList2: true,
      titleList2: '全部状态',
      child: [{
          title: '全部状态',
          selected: true
        },
        {
          title: '降价中',
          selected: false
        }, {
          title: '低库存',
          selected: false
        }, {
          title: '失效',
          selected: false
        }
      ]
    },
    typeList3: {
      showList3: true,
      titleList3: '本月',
      child: [{
        title: '按月选择',
        selected: true,
        dateChan: 'month'
      }, {
        title: '按日选择',
        selected: false,
        dateChan: 'day'
      }, ]
    },
    billMsg: [{
        name: '智能自动洗菜机',
        date: '2019-07-31',
        price: '300.00',
        discount: '60.00'
      },
      {
        name: '智自动按摩耳机呢任务二二月河特后台郭文贵为个位',
        date: '2019-07-31',
        price: '888.00',
        discount: '10.00'
      },
      {
        name: '智能自动洗菜机',
        date: '2019-07-31',
        price: '300.00',
        discount: '60.00'
      },
      {
        name: '智能自动洗菜机',
        date: '2019-07-31',
        price: '300.00',
        discount: '60.00'
      },
      {
        name: '智能自动洗菜机',
        date: '2019-07-31',
        price: '300.00',
        discount: '60.00'
      },
      {
        name: '智能自动洗菜机',
        date: '2019-07-31',
        price: '300.00',
        discount: '60.00'
      },
      {
        name: '智能自动洗菜机',
        date: '2019-07-31',
        price: '300.00',
        discount: '60.00'
      },
      {
        name: '智能自动洗菜机',
        date: '2019-07-31',
        price: '300.00',
        discount: '60.00'
      },
      {
        name: '智能自动洗菜机',
        date: '2019-07-31',
        price: '300.00',
        discount: '60.00'
      },
      {
        name: '智能自动洗菜机',
        date: '2019-07-31',
        price: '300.00',
        discount: '60.00'
      },
    ],
  },

  countPrice(e) { //计算总价
    let countPrice = 0 //存放临时总价
    let disPrice = 0 //存放

    // this.setData({
    //   totalDisPrice: 0,
    //   totalPrice: 0,
    // })
    for (let i in this.data.billMsg) {//存入总价
      // console.log(this.data.totalDisPrice)
      countPrice += parseFloat(this.data.billMsg[i].price)
      disPrice += parseFloat(this.data.billMsg[i].discount)

      // this.setData({
      //   totalDisPrice: this.data.totalDisPrice + parseFloat(this.data.billMsg[i].discount),
      //   totalPrice: this.data.totalPrice + parseFloat(this.data.billMsg[i].price),
      // })

    }
    // console.log(countPrice, disPrice)

    this.setData({//遍历完再赋值（优化性能）
      totalPrice: countPrice,
      totalDisPrice: disPrice
    })

  },

  hiddenItem(e) { //点击屏幕隐藏菜单（改变选项上下箭头）
    // console.log(e)
    this.setData({
      ['typeList1.showList1']: true,
      ['typeList2.showList2']: true,
      ['typeList3.showList3']: true,
      imgList1: '../../images/downImg.png',
      imgList2: '../../images/downImg.png',
      imgList3: '../../images/downImgW.png',
    })
  },
  checkType(e) { //下拉菜单(显示、隐藏菜单并改变箭头)
    // console.log(e.currentTarget.dataset.type)
    if (e.currentTarget.dataset.type == 1) { //区分选项
      this.setData({
        ['typeList1.showList1']: !this.data.typeList1.showList1
      })
      // console.log(this.data.typeList1.showList1)
      if (this.data.typeList1.showList1 == true) {
        this.setData({
          imgList1: '../../images/downImg.png',
          imgList2: '../../images/downImg.png',
          imgList3: '../../images/downImgW.png',
        })
      } else {
        this.setData({
          imgList1: '../../images/upImg.png',
          ['typeList2.showList2']: true,
          ['typeList3.showList3']: true,
          imgList2: '../../images/downImg.png',
          imgList3: '../../images/downImgW.png',
        })
      }
    } else if (e.currentTarget.dataset.type == 2) {
      this.setData({
        ['typeList2.showList2']: !this.data.typeList2.showList2
      })
      // console.log(this.data.typeList1.showList1)
      if (this.data.typeList2.showList2 == true) {
        this.setData({
          imgList2: '../../images/downImg.png',
          imgList1: '../../images/downImg.png',
          imgList3: '../../images/downImgW.png',
        })
      } else {
        this.setData({
          imgList2: '../../images/upImg.png',
          ['typeList1.showList1']: true,
          ['typeList3.showList3']: true,
          imgList1: '../../images/downImg.png',
          imgList3: '../../images/downImgW.png',
        })
      }
    } else if (e.currentTarget.dataset.type == 3) {
      this.setData({
        ['typeList3.showList3']: !this.data.typeList3.showList3
      })
      // console.log(this.data.typeList1.showList1)
      if (this.data.typeList3.showList3 == true) {
        this.setData({
          imgList2: '../../images/downImg.png',
          imgList1: '../../images/downImg.png',
          imgList3: '../../images/downImgW.png',
        })
      } else {
        this.setData({
          imgList2: '../../images/downImg.png',
          ['typeList1.showList1']: true,
          ['typeList2.showList2']: true,
          imgList1: '../../images/downImg.png',
          imgList3: '../../images/upImgW.png',
        })
      }
    }

  },


  checkItem(e) { //选择菜单项
    // console.log(e.currentTarget.dataset.type)
    if (e.currentTarget.dataset.type == 1) { //区分选项
      for (let i in this.data.typeList1.child) {

        console.log(i)
        this.setData({
          ['typeList1.child[' + i + '].selected']: false,
          ['typeList1.child[' + e.currentTarget.dataset.id + '].selected']: true,
          ['typeList1.titleList1']: this.data.typeList1.child[e.currentTarget.dataset.id].title,
          ['typeList1.showList1']: true
        })

        this.setData({
          imgList1: '../../images/downImg.png',
          imgList2: '../../images/downImg.png',
          imgList3: '../../images/downImgW.png',
        })
      }
    } else if (e.currentTarget.dataset.type == 2) {
      for (let i in this.data.typeList2.child) {

        // console.log(i)
        this.setData({
          ['typeList2.child[' + i + '].selected']: false,
          ['typeList2.child[' + e.currentTarget.dataset.id + '].selected']: true,
          ['typeList2.titleList2']: this.data.typeList2.child[e.currentTarget.dataset.id].title,
          ['typeList2.showList2']: true
        })

        this.setData({
          imgList2: '../../images/downImg.png',
          imgList1: '../../images/downImg.png',
          imgList3: '../../images/downImgW.png',

        })
      }
    } else if (e.currentTarget.dataset.type == 3) {
      for (let i in this.data.typeList3.child) {

        console.log(i)
        this.setData({
          ['typeList3.child[' + i + '].selected']: false,
          ['typeList3.child[' + e.currentTarget.dataset.id + '].selected']: true,
          ['typeList3.titleList3']: this.data.typeList3.child[e.currentTarget.dataset.id].title,
          ['typeList3.showList3']: true
        })

        this.setData({
          imgList2: '../../images/downImg.png',
          imgList1: '../../images/downImg.png',
          imgList3: '../../images/downImgW.png',
        })
      }
    }
    // console.log('菜单项')
    this.countPrice()
  },
  bindDateChange: function(e) { //选择日期
    this.setData({
      ['typeList3.titleList3']: e.detail.value
    })
    this.countPrice()
    // console.log('选择日期')
  },
  // changeEdit(e) {
  //   console.log(e)
  //   this.setData({
  //     hiddenEdit: !this.data.hiddenEdit,
  //   })
  // },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.countPrice()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})